package com.nartosoft.tubuloader;

import java.io.IOException;
import java.io.RandomAccessFile;

public class Utils {
	/**
	 * Reads the whole content of the specified file.
	 * 
	 * @param fileName
	 * @return A String containing the file content or null if an error occured.
	 */
	public static String readFile(String fileName) {
		RandomAccessFile file = null;
		try {
			file = new RandomAccessFile(fileName, "r");
			byte[] data = new byte[(int) file.length()];
			file.read(data);
			return new String(data);
		}
		catch (IOException exc) {
			exc.printStackTrace();
			return null;
		}
		finally {
			try {
				if (file != null) {
					file.close();
				}
			}
			catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * @return The file extension of fileName or an empty string if fileName has 
	 * no file extension.
	 */
	public static String getFileExtension(String fileName) {
		final int startOfExtension = fileName.lastIndexOf('.');
		if (startOfExtension == -1) {
			return "";
		}
		return fileName.substring(startOfExtension + 1);		
	}
}
